let logger = require("pomelo-logger").getLogger(__filename);
const ANNOUNCEMENT_COLLECTION_NAME = "announcement";

let Dao = function (app) {
    this.app = app;
};

Dao.prototype.findById = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find announcement failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOne({id : id}, function (error, document) {
            if(!!error) {
                logger.error(`findById in announcements failed. error=${error}`);
                callback(null);
                return;
            }
            callback(document);
        });
    });
};

Dao.prototype.findAllAnnouncements = function (callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find announcement in announcements failed. get collection error. error=${error}`);
            callback([]);
        }
        collection.find({}).toArray(function (error, documents) {
            if(error){
                logger.error(`findAllAnnouncements in announcements failed. error=${error}`);
                callback([]);
                return;
            }
            callback(documents);
        });
    });
};

Dao.prototype.findEffectAnnouncements = function (ts, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find announcement in announcements failed. get collection error. error=${error}`);
            callback([]);
        }
        collection.find({startTime: {$lt : ts}, endTime: {$gt : ts}}).toArray(function (error, documents) {
            if(error){
                logger.error(`findEffectAnnouncements in announcements failed. error=${error}`);
                callback([]);
                return;
            }
            callback(documents);
        });
    });
};

Dao.prototype.insertOne = function (document, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`insert announcement in announcements failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.insertOne(document, function (error, result) {
            if (!!error) {
                logger.error(`insert announcement in announcementss failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};


Dao.prototype.update = function (id, title, content, startTime, endTime, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update announcement in announcements failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne(
            {id : id},
            {$set:{title : title, content : content, startTime : startTime, endTime : endTime}},
            function (error, result) {
                if (!!error) {
                    logger.error(`update announcement in announcements failed. error=${error}`);
                    callback(null);
                    return;
                }
                callback(result);
        });
    });
};

Dao.prototype.deleteById = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(ANNOUNCEMENT_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`delete announcement in announcements failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOneAndDelete({id : id}, function (error, result) {
            if (!!error) {
                logger.error(`delete announcement in announcements failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};


module.exports.create = function (app) {
    return new Dao(app);
};
